﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using PLM.ConfigurationCenter.Backup.Handle;
using PLM.ConfigurationCenter.Model;
using PLM.ConfigurationCenter.Service;
using static PLM.ConfigurationCenter.Model.Extensions;

namespace PLM.ConfigurationCenter.Backup.CustomForms {
    public partial class BackupForm : Form {

        private TaskService _backupService = new TaskService();

        private TaskModel BackupData { get; set; }

        public BackupForm(TaskModel? model = null) {
            InitializeComponent();
            BackupData = model ?? new TaskModel();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BackupForm_Load(object sender, EventArgs e) {
            BindTimes();
            LoadData();
            this.listView1.HeaderStyle = ColumnHeaderStyle.Clickable;
            this.listView1.View = View.Details;
            this.listView1.Columns.Add("执行时间", 150, HorizontalAlignment.Center);

        }
        /// <summary>
        /// 
        /// </summary>
        private void BindTimes() {
            var list = new List<TimesModel>();
            foreach (ExcTimes excTimes in Enum.GetValues(typeof(ExcTimes))) {
                var m = new TimesModel { key = (int)excTimes, Name = excTimes.ToString() };
                list.Add(m);
            }
            this.cbb_Times.DataSource = list;
            this.cbb_Times.DisplayMember = "Name";
            this.cbb_Times.ValueMember = "key";
            this.cbb_Times.SelectedIndex = 0;
        }
        private void BindExecuteList() {
            int Times = ((TimesModel)this.cbb_Times.SelectedItem).key;
            this.listView1.Items.Clear();
            //数据更新，UI暂时挂起，直到EndUpdate绘制控件，可以有效避免闪烁并大大提高加载速度  
            this.listView1.BeginUpdate();
            //添加5行数据  
            DateTime start = Convert.ToDateTime(DateTime.Now.ToString("D").ToString());
            DateTime temp = start.AddHours(1);
            for (int i = 0; i < 5; i++) {
                ListViewItem lvi = new ListViewItem();

                lvi.ImageIndex = i;     //通过与imageList绑定，显示imageList中第i项图标  
                temp = temp.AddDays(Times);
                lvi.Text = temp.ToLongDateString() + " " + temp.ToLongTimeString();

                //lvi.SubItems.Add("第2列,第" + i + "行");

                //lvi.SubItems.Add("第3列,第" + i + "行");

                this.listView1.Items.Add(lvi);
            }

            this.listView1.EndUpdate();  //结束数据处理，UI界面一次性绘制。  
        }
        /// <summary>
        /// 
        /// </summary>
        private void LoadData() {
            if (string.IsNullOrEmpty(BackupData.UuId)) {
                return;
            }
            this.tb_Server.Text = BackupData.ServerAddress;
            this.tb_DBName.Text = BackupData.DataBase;
            this.cb_Yes.Checked = BackupData.IsUse;
            this.tb_DBPassword.Text = BackupData.Password;
            this.tb_DBUser.Text = BackupData.Username;
            this.cbb_Times.SelectedValue = BackupData.Times;

            if (this.BackupData.DataBaseType == 2) {
                this.rbtn_Oracle.Checked = true;
            } else if (BackupData.DataBaseType == 1) {
                this.rbtn_SqlServer.Checked = true;
            } else {
                this.rbtn_PostgreSQL.Checked = true;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Save_Click(object sender, EventArgs e) {
            BackupData.ServerAddress = this.tb_Server.Text.Trim();
            BackupData.DataBase = this.tb_DBName.Text.Trim();
            BackupData.IsUse = this.cb_Yes.Checked;
            BackupData.Password = this.tb_DBPassword.Text.Trim();
            BackupData.PostTime = DateTime.Now;
            BackupData.Username = this.tb_DBUser.Text.Trim();
            BackupData.Times = ((TimesModel)this.cbb_Times.SelectedItem).key;


            if (this.rbtn_Oracle.Checked) {
                BackupData.DataBaseType = 2;
            } else if (this.rbtn_SqlServer.Checked) {
                BackupData.DataBaseType = 1;
            } else {
                BackupData.DataBaseType = 3;
            }

            bool res = Common.CheckHealth(BackupData, out string error);
            if (!res) {
                MessageBox.Show(error);
                return;
            }
            if (string.IsNullOrEmpty(BackupData.UuId)) {
                BackupData.UuId = Guid.NewGuid().ToString("N");
                res = _backupService.Insert(BackupData);
            } else {
                res = _backupService.Update(BackupData);
            }
            if (!res) {
                MessageBox.Show("保存失败！");
                return;
            }
            this.DialogResult = DialogResult.OK;
            MessageBox.Show("保存成功！");
            this.Close();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cbb_Times_SelectedIndexChanged(object sender, EventArgs e) {
            BindExecuteList();
        }
    }

}
